package com.edu.service.impl;

import com.edu.beans.Book;
import com.edu.beans.Home;
import com.edu.dao.BookDao;
import com.edu.dao.HomeDao;
import com.edu.dao.impl.BookDaoImpl;
import com.edu.dao.impl.HomeDaoImpl;
import com.edu.service.BookService;
import com.edu.utils.JdbcUtils;

import java.util.List;

public class BookServiceImpl implements BookService {
    BookDao bookDao = new BookDaoImpl();
    HomeDao homeDao = new HomeDaoImpl();

    @Override
    public int addBook(Book book) {

        int count1 = 0;
        int count2 = 0;
        try {
            JdbcUtils.startTransaction();
            count1=JdbcUtils.executeUpdate("INSERT INTO tb_books VALUES (NULL, ?, ?, ?, ?, NOW())",
                    book.getVip_id(), book.getEnterTime(), book.getExitTime(), book.getHome_id());
            //跨了两个表，有事务问题
            //count1 = bookDao.addBook(book);
            Home home = homeDao.getHomeById(book.getHome_id());
            //home.setState("已入住");
            count2=JdbcUtils.executeUpdate("update tb_home set num = ?, h_type = ?, price = ?, state = ?, img = ?, text = ?,updatetime=now() where id = ?",
                    home.getNum(), home.getH_type(), home.getPrice(), "已入住", home.getImg(), home.getText(), home.getId());
            JdbcUtils.commit();
            JdbcUtils.closeConnection();
        } catch (Exception e) {
            JdbcUtils.rollback();
        }

        return count1 + count2 > 1 ? 1 : 0;
    }

    @Override
    public int deleteBook(String id) {

        return bookDao.deleteBook(id);
    }

    @Override
    public Book getBookById(String id) {
        return bookDao.getBookById(id);
    }


    /**
     * 更新预定
     * @param book
     * @return
     */
    @Override
    public int updateBook(Book book) {
        int count1 = 0;
        int count2 = 0;
        int count3 = 0;

        //获取更新之前的房间ID
        Book book1 = bookDao.getBookById(String.valueOf(book.getId()));
        if (book1.getHome_id() != book.getHome_id()) {
            try {
                //跨了两个表，有事务问题
                Home home1 = homeDao.getHomeById(book1.getHome_id());
                JdbcUtils.startTransaction();
//            home1.setState("未打扫");
//            count2 = homeDao.updateHome(home1);
                count2=JdbcUtils.executeUpdate("update tb_home set num = ?, h_type = ?, price = ?, state = ?, img = ?, text = ?,updatetime=now() where id = ?",
                        home1.getNum(), home1.getH_type(), home1.getPrice(), "未打扫", home1.getImg(), home1.getText(), home1.getId());
                Home home2 = homeDao.getHomeById(book.getHome_id());
                count3=JdbcUtils.executeUpdate("update tb_home set num = ?, h_type = ?, price = ?, state = ?, img = ?, text = ?,updatetime=now() where id = ?",
                        home2.getNum(), home2.getH_type(), home2.getPrice(), "已入住", home2.getImg(), home2.getText(), home2.getId());
//            home2.setState("已入住");
//            count3 = homeDao.updateHome(home2);
//            count1 = bookDao.updateBook(book);
                count1=JdbcUtils.executeUpdate("update tb_books set entertime = ?, exittime = ?, home_id = ? where id = ?",
                        book.getEnterTime(), book.getExitTime(), book.getHome_id(), book.getId());
                JdbcUtils.commit();
                JdbcUtils.closeConnection();
            } catch (Exception e) {
                JdbcUtils.rollback();
            }
            return count1+count2+count3 > 2 ? 1 : 0;
        }
        count1=JdbcUtils.executeUpdate("update tb_books set entertime = ?, exittime = ?, home_id = ? where id = ?",
                book.getEnterTime(), book.getExitTime(), book.getHome_id(), book.getId());

        return count1;
    }

    @Override
    public List<Book> getBookByVipId(int vipId) {
        return bookDao.getBooksByVipId(vipId);
    }
}
